package main

import (
	"fmt"
	"go_code/rpc/protoc/demo"
	"google.golang.org/protobuf/proto"
	"io/ioutil"
)

func main() {
	p1 := demo.Parent{
		Name:   "Alice",
		Age: 25,
		Address: "AnHui",
	}
	fmt.Println("序列化对象：",p1)
	// 序列化
	data, err := proto.Marshal(&p1)
	if err != nil {
		fmt.Printf("序列化异常,err:%v\n", err)
		return
	}
	//将序列化数据写入到文件中，并设置自己读写其他读权限
	ioutil.WriteFile("./person.txt", data, 0644)
	data2, err := ioutil.ReadFile("./person.txt")
	if err != nil {
		fmt.Printf("读取文件异常, err:%v\n", err)
		return
	}
	//从文件中反序列化到对象中
	var p2  demo.Person
	proto.Unmarshal(data2, &p2)
	fmt.Println("反序列化对象：",p2)
}
